<template>
  <div class="box app-container">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never" :body-style="{height:'calc(100vh - 230px)'}">
          <el-col :span="24">
            <div style="float: left;">
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-button type="primary" @click="dialogVisible=true;dialogTitle='添加';resetForm('ruleForm')" icon="el-icon-plus"
                             size="medium"
                  >添 加
                  </el-button>
                </el-form-item>
                <el-form-item>
                  <el-button type="danger" @click="deleteItem" icon="el-icon-delete" size="medium">批量删除</el-button>
                </el-form-item>
              </el-form>
            </div>
            <div style="float:right;">
              <el-form :inline="true" class="demo-form-inline" v-model="formInline">
                <el-form-item>
                  <el-input v-model="formInline.username" placeholder="请输入角色码" size="medium"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="searchItem" size="medium">查询</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <el-col :span="24">
            <el-table
              :data="tableData.slice((pages.pageNum-1)*pages.pageSize,pages.pageNum*pages.pageSize)"
              style="width: 100%;min-height: 440px;"
              row-key="name"
              border
              default-expand-all
              @selection-change="handleSelectionChange"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            >
              <el-table-column
                type="selection"
                width="55"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                label="名称"
              >
              </el-table-column>
              <el-table-column
                prop="id"
                label="父节点"
                width="120"
              >
              </el-table-column>
              <el-table-column
                prop="date"
                label="创建时间"
              >
              </el-table-column>
              <el-table-column
                prop="address"
                label="地址"
              >
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="handleEdit(scope.row)"
                  >编辑
                  </el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <Pagination :total="total" :pageNum.sync="pages.pageNum"
                        :limit.sync="pages.pageSize"
                        :pageSizes="pageSizes"
            ></Pagination>
          </el-col>
          <el-dialog
            :title="dialogTitle"
            class="dialog"
            :visible.sync="dialogVisible"
            width="30%"
          >
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="父节点" required prop="value">
                <el-select v-model="ruleForm.value" placeholder="请选择"
                           class="tree"
                           :popper-append-to-body="false"
                >
                  <el-option :value="selectTree" class="setStyle" disabled>
                    <el-tree :data="dataList" :props="defaultProps"
                             check-on-click-node @node-click="handleNodeClick"
                    ></el-tree>
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
              <el-form-item label="创建时间" required>
                <el-col :span="11">
                  <el-form-item prop="date1">
                    <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col class="line" :span="2" style="text-align: center">-</el-col>
                <el-col :span="11">
                  <el-form-item prop="date2">
                    <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
            </span>
          </el-dialog>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination/Pagination'

export default {
  name: 'index',
  components: { Pagination },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogTitle: '',
      selectTree: [],
      ruleForm: {
        value: '',
        date1: '',
        date2: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请选择父节点', trigger: 'blur' }
        ],
        date1: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        date2: [
          { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
        ]
      },
      multipleSelection: [],
      tableData: [],
      dialogVisible: false,
      formInline: {
        username: ''
      },
      dataList: [],
      total: 0,
      pages: {
        pageNum: 1,
        pageSize: 10
      },
      pageSizes: [10, 20, 30, 40, 50, 100],
      loading: false,
      breadList:[]
    }
  },
  methods: {
    resetForm(formName) {
      this.$nextTick(()=>{
        this.$refs[formName].resetFields();
      })
    },
    handleNodeClick(data){
      this.ruleForm.value = data.name
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          })
          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    deleteItem() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error('未选中任何行')
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '模拟删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    searchItem() {
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].name.indexOf(this.formInline.username) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.resetForm('ruleForm')
     this.$nextTick(()=>{
       this.dialogTitle = '编辑'
       this.ruleForm.value = row.parent
       this.ruleForm.name = row.name
       this.ruleForm.date1 = new Date(row.date)
       this.ruleForm.date2 = new Date(row.date)
     })
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '模拟删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    fetchData() {
      //请求接口加载表格数据
      let list = [
        {
          id: 'root',
          date: '1985-04-05 14:28:38',
          name: '根节点',
          parent:'根节点',
          address: '上海市普陀区金沙江路 1519 弄',
          children: [
            {
              id: 'admin',
              date: '2006-04-21 19:48:08',
              name: '桥本有菜',
              parent:'根节点',
              address: '上海市普陀区金沙江路 1519 弄',
              children: [
                {
                  id: 'admin',
                  date: '1989-11-24 12:24:10',
                  name: '明里紬',
                  parent:'桥本有菜',
                  address: '上海市普陀区金沙江路 1519 弄'
                },
                {
                  id: 'admin',
                  date: '1994-12-21 13:27:48',
                  name: '大桥未久',
                  parent:'桥本有菜',
                  address: '上海市普陀区金沙江路 1519 弄'
                }
              ]
            },
            {
              id: 'admin',
              date: '2014-08-15 11:54:10',
              name: '河北彩花',
              parent:'根节点',
              address: '上海市普陀区金沙江路 1519 弄',
              children: [
                {
                  id: 'admin',
                  date: '1995-05-09 08:07:40',
                  name: '凉森玲梦',
                  parent:'河北彩花',
                  address: '上海市普陀区金沙江路 1519 弄'
                },
                {
                  id: 'admin',
                  date: '1990-08-02 04:27:40',
                  name: '深田咏美',
                  parent:'河北彩花',
                  address: '上海市普陀区金沙江路 1519 弄'
                },
                {
                  id: 'admin',
                  date: '1999-04-03 01:47:05',
                  name: '三上悠亚',
                  parent:'河北彩花',
                  address: '上海市普陀区金沙江路 1519 弄'
                }
              ]
            }
          ]
        }
      ]
      this.tableData = list
      this.dataList = list
      this.total = list.length
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
.box {
  margin-bottom: 50px;

  ::v-deep.el-form-item__label {
    font-weight: 400;
  }

  ::v-deep .has-gutter {
    tr {
      th {
        background-color: #F5F7FA;
      }
    }
  }

  ::v-deep.cell {
    font-size: 14px;
    font-weight: 400;
    text-align: center;
  }

  .setStyle {
    height: auto;
    padding: 0 !important;
    margin: 0;
    overflow: auto;
    cursor: default !important;
  }

  .dialog {
    ::v-deep .el-scrollbar__wrap {
      overflow: unset;
    }
  }
}
</style>
